package com.example.testprog1;

import com.example.testprog1.MyTables.AccountType;

import android.content.*;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.*;

public class MyDBOpenHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "TestProg_DB";
	private static final int DATABASE_VERSION = 1;
	
	/*
	private static final String DATABASE_CREATE_CATEGORY = "" +
			"CREATE TABLE \"category\" (" +
			"\"category_id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," +
			"\"category_name\" VARCHAR," +
			"\"category_type\" INTEGER," +
			"\"category_parent\" VARCHAR," +
			"\"category_description\" VARCHAR," +
			")";
	/*
	private static final String DATABASE_CREATE_CATEGORY_TYPE = "" +
			"CREATE TABLE \"category_type\" (" +
			"\"category_type_id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ," +
			"\"category_type_name\" VARCHAR," +
			")";
	*/
	
	
	//************************************************************
	private static final String TEXT_TYPE = " TEXT";
	private static final String COMMA_SEP = ",";
	private static final String SQL_CREATE_ACCOUNT_TYPE_TABLE =
	    "CREATE TABLE " + AccountType.TABLE_NAME + " (" +
	    AccountType._ID + " INTEGER PRIMARY KEY," +
	    AccountType.COLUMN_NAME_ID + " INT" + COMMA_SEP +
	    AccountType.COLUMN_NAME_ACCOUNT_TYPE_NAME + TEXT_TYPE +
	    // Any other options for the CREATE command
	    " )";

	//************************************************************
	
	//public MyDBOpenHelper(Context context,int DATABASE_VERSION) {
	public MyDBOpenHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	//Create Tables
	@Override
	public void onCreate (SQLiteDatabase DB){
		//DB.execSQL(DATABASE_CREATE_CATEGORY);
		//DB.execSQL(DATABASE_CREATE_CATEGORY_TYPE);
		DB.execSQL(SQL_CREATE_ACCOUNT_TYPE_TABLE);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	
	public SQLiteDatabase getDatabase(){
		return this.getWritableDatabase();
	}
	
	public boolean closeAllDatabase(){
		try{
			this.close();
		}
		catch (Exception e) {
			return false;
		}
		return true;
	}
	
	//Querry
	public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
		SQLiteDatabase database = this.getWritableDatabase();
		Cursor result = null;
		try {
			result = database.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			database.close();
		}
		return result;
	}
	
	//Update
	public long update(String tableName,ContentValues values,String whereClause, String[] params){
		int count = 0;
		SQLiteDatabase database = this.getWritableDatabase();
		try{
			count = database.update( tableName, values, whereClause, params);
		} catch(Exception e){
			e.printStackTrace();
		} finally {
			database.close();
		}
		return count;
	}
	
	//Insert
	public long insert(String tableName,ContentValues values){
		SQLiteDatabase database = this.getWritableDatabase();
		long count = database.insert(tableName, null, values);
		return count;
	}
	
	//Delete
	public int deleteRow(String table,String clause,String[] params){
		SQLiteDatabase database = getDatabase();
		int count = 0;
		try{
			count = database.delete(table, clause, params);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			database.close();
		}
		return count;
	}
	
}


